﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Phones.Client.Model;
using System.Collections.ObjectModel;
using Phones.Client.Repository;
using System.Data.Objects;

namespace Phones.Client.Repository
{
    public class OrganisationsRepository : BaseRepository<Organisation>, IDisposable
    {

        public OrganisationsRepository(PhonesEntities context) : base(context) { }
        public OrganisationsRepository() : base() { }

        public List<Organisation> GetList(ServiceNode node)
        {
            if (node == null) return null;
            else
            {
                var query = from org in Context.Organisations.Include("Departments")
                            join dep in Context.Departments on org.ID equals dep.Organisation.ID
                            join c in Context.Contacts on dep.ID equals c.Department.ID
                            where c.City.ServiceNode.ID == node.ID
                            orderby org.Name ascending
                            select org;
                var list = query
                            .Distinct()
                            .ToList();
                //list.ForEach((x) => Context.Detach(x));
                return list;
            }
        }
    }
}
